/**
 * 服务类型
 */
var ServiceType = {
    id: "ServiceTypeTable",//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ServiceType.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '服务编码', field: 'service_code', align: 'center', valign: 'middle', sortable: true},
        {title: '服务名称', field: 'service_name', align: 'center', valign: 'middle', sortable: true},
        {title: '持续时间（天）', field: 'duration', align: 'center', valign: 'middle', sortable: true},
        {title: '服务价格', field: 'price', align: 'center', valign: 'middle', sortable: true}];
    return columns;
};

/**
 * 检查是否选中
 */
ServiceType.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ServiceType.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
ServiceType.openAdd = function () {
    var index = layer.open({
        type: 2,
        title: '添加',
        area: ['500px', '550px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/servicetype/servicetype_add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 * @param userId
 */
ServiceType.openChange = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '编辑',
            area: ['500px', '550px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/servicetype/servicetype_edit/' + this.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除
 */
ServiceType.del = function () {
    if (this.check()) {

        var operation = function(){
            var id = ServiceType.seItem.id;
            var ajax = new $ax(Feng.ctxPath + "/servicetype/delete", function () {
                Feng.success("删除成功!");
                ServiceType.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", id);
            ajax.start();
        };

        Feng.confirm("是否删除?",operation);
    }
};

ServiceType.resetSearch = function () {
    $("#service_code").val("");
    ServiceType.search();
}

ServiceType.search = function () {
    var queryData = {};

    queryData['service_code'] = $("#service_code").val();
    ServiceType.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = ServiceType.initColumn();
    var table = new BSTable("ServiceTypeTable", "/servicetype/list", defaultColunms);
    table.setPaginationType("client");
    ServiceType.table = table.init();

});
